package com.zhuozhou.jdyc.main.main.adapter;

import android.content.Context;

import com.bumptech.glide.Glide;
import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.base.BaseRecyclerViewAdapter;
import com.zhuozhou.jdyc.base.BaseViewHolder;
import com.zhuozhou.jdyc.databinding.CartShopItemItemBinding;
import com.zhuozhou.jdyc.main.main.bean.BasicsShoppingBean;
import com.zhuozhou.jdyc.util.log.ToastUtils;

/**
 * Create by CherishTang on 2020/3/27 0027
 * describe:首页菜单adapter
 */
public class CartShopItemAdapter extends BaseRecyclerViewAdapter<BasicsShoppingBean.ObjBeanX.ObjBean.ShangpinBean, CartShopItemItemBinding> {
    public int goodWeight = 0;
    private int type = 0;
    public OnItemClickRadioListener mOnItemClickRadioListener;

    public CartShopItemAdapter(Context context) {
        super(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.cart_shop_item_item;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder baseViewHolder, int pos) {
        super.onBindViewHolder(baseViewHolder, pos);
        baseViewHolder.getBinding().getRoot().setOnClickListener(v -> {
            //前往商品详情
        });
    }

    @Override
    public void onBindHolder(BaseViewHolder<CartShopItemItemBinding> holder, int pos) {
        Glide.with(mContext).load(mList.get(pos).getSpec_image()).into(holder.getBinding().imageShop);
        holder.getBinding().setBean(mList.get(pos));
        if (type == 1) {
            holder.getBinding().setGoodsPrice("￥" + mList.get(pos).getGoods_price());
        } else if (type == 2) {
            holder.getBinding().setGoodsPrice(mList.get(pos).getIntegral() + "积分 + ￥" + mList.get(pos).getGoods_price() + "");
        } else {
            holder.getBinding().setGoodsPrice(mList.get(pos).getYibi() + "易货积分");
        }
        holder.getBinding().tvReduce.setOnClickListener(v -> {
            goodWeight = mList.get(pos).getNumber();
            if (goodWeight <= 0) {
                return;
            }
            if (goodWeight == 1) {
                ToastUtils.showShort(mContext, "该商品不能再减少了哦");
                return;
            }
            goodWeight--;
            mList.get(pos).setNumber(goodWeight);
            holder.getBinding().tvNum.setText(goodWeight + "");
            if (mOnItemClickRadioListener != null)
                mOnItemClickRadioListener.onTOnItemClickRadio(pos, -1);
        });
        holder.getBinding().tvAdd.setOnClickListener(v -> {
            goodWeight = mList.get(pos).getNumber();
            if (goodWeight <= 0) {
                return;
            }
            if (goodWeight >= mList.get(pos).getNumberlist()) {
                ToastUtils.showShort(mContext, "该商品限购" + mList.get(pos).getNumberlist() + mList.get(pos).getUnit());
                return;
            }
            goodWeight++;
            mList.get(pos).setNumber(goodWeight);
            holder.getBinding().tvNum.setText(goodWeight + "");
            if (mOnItemClickRadioListener != null)
                mOnItemClickRadioListener.onTOnItemClickRadio(pos, -1);
        });
        holder.getBinding().radioShop.setOnCheckedChangeListener((buttonView, isChecked) -> {
            mList.get(pos).setChecked(isChecked);
            if (mOnItemClickRadioListener != null)
                mOnItemClickRadioListener.onTOnItemClickRadio(pos, pos);
        });
        holder.getBinding().radioShop.setChecked(mList.get(pos).isChecked());
        holder.getBinding().tvDelete.setOnClickListener(v -> {
            mOnItemClickRadioListener.onTOnItemClickRadio(pos, -2);
            holder.getBinding().swipeItemLayout.closeWithAnim();
        });
        holder.getBinding().imageShop.setOnClickListener(v -> {
            mOnItemClickRadioListener.onTOnItemClickRadio(pos, -3);
        });
    }

    public void setOnItemClickRadioListener(OnItemClickRadioListener l) {
        mOnItemClickRadioListener = l;
    }

    public interface OnItemClickRadioListener {
        void onTOnItemClickRadio(int pos, int position);
    }

    public void setType(int type) {
        this.type = type;
    }
}
